package br.com.roteadroid;

import android.annotation.SuppressLint;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.util.Log;
import br.com.roteadroid.util.HttpTool;
import br.com.roteadroid.vo.LocalizacaoVO;
import com.google.gson.Gson;
import com.google.inject.Inject;

public class Servico extends IntentService {
	
	 public Servico(String name) {
		super(name);
	}
	 
	@SuppressLint("Instantiatable")
	public Servico() {super("");}

	public static final String CATEGORIA = "servico";
	 @Inject HttpTool httpTool;
	 GPSTracker gps;
	 
	/* @Override
	 public void onStart(Intent intent, int startId) {
		 Log.d("servico", "iniciou servico");
		 httpTool = new HttpTool();
		 gps = new GPSTracker(this);
		 incremento(0);
		 stopSelf();
	 }*/
	 
	 private void incremento(int result) {
		while(true){
			 try {
				 Thread.sleep(60000);
				 if(gps.canGetLocation()){
					 gps.getLocation();
		             double latitude = gps.getLatitude();
		             double longitude = gps.getLongitude();
		             TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		             String imei = tm.getDeviceId();
		             String resposta;
		             Gson gson = new Gson();
		             try {
		            	 LocalizacaoVO vo = new LocalizacaoVO();
		            	 vo.setLatitude(latitude+"");
		            	 vo.setLongitude(longitude+"");
		            	 vo.setImei(imei);
		            	 String json = gson.toJson(vo);
		            	 //resposta = httpTool.executaPostJson("10.0.2.2:8080/roteadorweb", "/roteadorweb/localizacao", json);
		            	 resposta = httpTool.executaPostJson("roteadorweb-env.elasticbeanstalk.com", "/localizacao", json);
		            	 //http:///
		            	 //resposta = httpTool.executaPost("maxsolutiongroup.com/mgabriel", "/locator/recebe.php", preencheListaCampos(latitude, longitude, imei));
		 				//resposta = httpTool.executaPost("10.171.144.64", "/locator/recebe.php", preencheListaCampos(latitude, longitude, imei));
		 			} catch (Exception e) {
		 				resposta = e.getMessage();
		 				Log.d("ERRO", resposta);
		 			}
		             Log.d("MSG: ", "Your Location is - \nLat: " + latitude + "\nLong: " + longitude + "\nImei: " + imei);
		             //Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude + "\nImei: " + imei, Toast.LENGTH_LONG).show();   
		         }else{
		             // can't get location
		             // GPS or Network is not enabled
		             // Ask user to enable GPS/network in settings
		             gps.showSettingsAlert();
		         }
				 result++;
				 Log.i(CATEGORIA, "incremento  " + result);
			 } catch (InterruptedException e) {
				 Log.i(CATEGORIA, "erro " + e);
			 }
		 }
	 }
	 
	 @Override
	 public IBinder onBind(Intent intent) {
		 return null;
	 }

	@Override
	protected void onHandleIntent(Intent intent) {
		 Log.d("servico", "iniciou servico");
		 httpTool = new HttpTool();
		 gps = new GPSTracker(this);
		 incremento(0);
		 stopSelf();
	}
}